import { request } from './request.js'
request.defaultData.baseUrl = 'http://cyjs.pzhkj.cn/'

export function login(data) {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/login',
        method: 'POST',
        data,
        afterAjax: res => {
          uni.setStorageSync('unlogin', '0')
          uni.setStorageSync('Cookie', `PHPSESSID=${res.data}`)
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function getInfo(data) {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/getInfo',
        method: 'GET',
        data,
        afterAjax: res => {
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function updateInfo({
  insert_time,
  m_avatar,
  m_id,
  m_login_name,
  m_mobile,
  m_real_name,
  m_shenfenzheng,
  m_sheng,
  m_shi,
  m_qu,
}) {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/updateInfo',
        method: 'POST',
        data: {
          nickname: !!m_login_name ? m_login_name : undefined,
          phone: !!m_mobile ? m_mobile : undefined,
          identity_card: !!m_shenfenzheng ? m_shenfenzheng : undefined,
          avatar: !!m_avatar ? m_avatar : undefined,
          real_name: !!m_real_name ? m_real_name : undefined,
          sheng: !!m_sheng ? m_sheng : undefined,
          shi: !!m_shi ? m_shi : undefined,
          qu: !!m_qu ? m_qu : undefined,
        },
        afterAjax: res => {
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function resetPassword(data) {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/resetPassword',
        method: 'POST',
        data,
        afterAjax: res => {
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function submitFeedBack(data) {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/feedback',
        method: 'POST',
        data,
        afterAjax: res => {
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function logout() {
  return new Promise((resolve, reject) => {
    request
      .ajax({
        path: 'api/customers/logout',
        method: 'POST',
        afterAjax: res => {
          resolve(res)
        },
      })
      .catch(err => {
        reject(err)
      })
  })
}
